// pages/index/guessing_competition/guessing_competition.js
import Toast from "../../../miniprogram_npm/@vant/weapp/toast/toast"
import Dialog from "../../../miniprogram_npm/@vant/weapp/dialog/dialog"

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageNumber: 1,
        pageSize: 10,
        isRefresh: true,
        isLoading: true,
        guessingList: [],
        title: "",
        inputShowed: false,  //初始文本框不显示内容
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.onRefresh();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    /**
     * 输入内容
     * @param e
     */
    onSearchInput: function (e) {
        const name = e.detail.value
        this.setData({
            title: name
        })
    },
    /**
     * 下拉刷新
     * @param e
     */
    onRefresh: function (e) {
        this.data.pageNumber = 1
        this.data.guessingList = []
        this.getGuessingList()
    },
    /**
     * 上拉加载更多
     * @param e
     */
    onLower: function (e) {
        this.data.pageNumber += 1
        this.getGuessingList()
    },
    /**
     * 开始搜索
     * @param e
     */
    onSearch: function (e) {
        this.data.pageNumber = 1
        this.data.guessingList = []
        this.getGuessingList()
    },
    /**
     * 使文本框进入可编辑状态
     */
    showInput: function () {
        this.setData({
            inputShowed: true   //设置文本框可以输入内容
        });
    },
    /**
     * 取消搜索
     */
    hideInput: function () {
        this.setData({
            inputShowed: false
        });
    },
    getGuessingList: function (e) {
        this.setData({
            isLoading: true
        })
        const than = this
        http.get("questions/open/getAppletQuestions", {
            area_code: app.globalData.areaCode,
            questionnaire_type: 3,
            paper_title: than.data.title,
            page_number: than.data.pageNumber,
            page_size: than.data.pageSize,
            applet_user_id: app.globalData.userInfo.applet_user_id
        }).then(res => {
            if (res.data.code === 200) {
                res.data.data.list.forEach(item => {
                    than.data.guessingList.push(item)
                })
                than.setData({
                    guessingList: than.data.guessingList,
                    isRefresh: false,
                    isLoading: false
                })
            } else {
                Dialog.alert({
                    title: "失败",
                    message: "获取失败\n" + res.data.msg
                }).then(() => {

                })
                than.setData({
                    isRefresh: false,
                    isLoading: false
                })
            }
            if (res.data.data.list.length == 0) {
                than.data.pageNumber--
            }
        }, err => {
            Toast.fail("网络异常")
            than.setData({
                isRefresh: false,
                isLoading: false
            })
        })
    },
    onGuessingListDetails: function (e) {
        const id = e.currentTarget.dataset.id
        let count = e.currentTarget.dataset.count
        if (count > 0) {
            Dialog.alert({
                title: "提示",
                message: "已经回答过了"
            }).then(() => {

            })
        } else {
            wx.navigateTo({
                url: './guessing_list_details/guessing_list_details?id=' + id,
            })
        }

    }
})